<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\enum\AccountEnum;
use app\enum\TokenEnum;
use app\service\client\AccountService;
use app\service\token\TokenInterface;
use app\service\token\TokenService;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;

class CheckTokenAccountWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('check_token_account_work');
        $this->setDescription('更新token和账户关系');
        $this->addArgument('media_id', Argument::REQUIRED, '媒体 1巨量  2快手');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $mediaId = intval($input->getArgument('media_id'));
        $output->writeln('=======更新token和账户关系开始' . date('Y-m-d H:i:s', time()) . '=======');
        try {
            $service = TokenService::getInstance($mediaId);
            if (! $service instanceof TokenInterface) {
                $output->writeln('=======更新token和账户关系失败: 服务错误' . date('Y-m-d H:i:s', time()) . '=======');
            }
            // 查询账户
            $accounts = AccountService::getAccountList($mediaId, AccountEnum::PUTTING_STATUS_OK);
            $tokens = $service->getMediaAppTokens(TokenEnum::TOKEN_STATUS_OK);
            if ($accounts && $tokens) {
                foreach ($accounts as $account) {
                    $accountId = $account->getKey();
                    foreach ($tokens as $token) {
                        $tokenId = $token->getKey();
                        // 设置失效状态
                        $canGet = $service->checkTokenAccount($token, $accountId);
                        if ($canGet) {
                            // 新增关系
                            if (! $service->setTokenAccount($tokenId, $accountId)) {
                                $output->writeln('=======新增token和账户关系失败: token_id:' . $tokenId . 'account_id:' . $accountId . ',' . $service::getError(0) . date('Y-m-d H:i:s', time()) . '=======');
                            }
                            continue;
                        }
                        // 删除关系
                        if (! $service->delTokenAccount($tokenId, $accountId)) {
                            $output->writeln('=======删除token和账户关系失败: token_id:' . $tokenId . 'account_id:' . $accountId . ',' . $service::getError(0) . date('Y-m-d H:i:s', time()) . '=======');
                        }
                    }
                }
            }
        } catch (\Exception $e) {
            dump($e);
            $output->writeln('=======更新token和账户关系失败' . $e->getMessage() . date('Y-m-d H:i:s', time()) . '=======');
        }
        $output->writeln('=======更新token和账户关系结束' . date('Y-m-d H:i:s', time()) . '=======');
        $output->writeln('success');
    }
}
